{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mido import MidiFile\n",
    "import sys\n",
    "import json\n",
    "import mido\n",
    "import numpy as np\n",
    "import random\n",
    "\n",
    "def midifile_to_dict(mid):\n",
    "    tracks = []\n",
    "    for track in mid.tracks:\n",
    "        tracks.append([vars(msg).copy() for msg in track])\n",
    "\n",
    "    return {\n",
    "        'ticks_per_beat': mid.ticks_per_beat,\n",
    "        'tracks': tracks,\n",
    "    }\n",
    "\n",
    "\n",
    "def midi_to_txt(data_dir, file_name):\n",
    "    song_name = file_name.replace('.mid','')\n",
    "    json_path = data_dir + '/' + song_name\n",
    "    txt_path = data_dir + '/' + song_name + '.txt'\n",
    "    mid = mido.MidiFile(data_dir + '/' + file_name)\n",
    "\n",
    "    #print(json.dumps(midifile_to_dict(mid), indent=2))\n",
    "    with open(json_path, 'w') as outfile:\n",
    "        json.dump(midifile_to_dict(mid), outfile)\n",
    "\n",
    "    #The tempo is microseconds per beat, that is bpm = 60/(temp*10^(-6))\n",
    "    #C4 is 60\n",
    "\n",
    "    with open(json_path, 'r') as f:\n",
    "         data = json.load(f)\n",
    "    tpb = data['note']\n",
    "    print(tpb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "480\n"
     ]
    }
   ],
   "source": [
    "midi_to_txt('.','A.mid')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
